<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small">
        <el-form-item label="角色名称">
          <el-input clearable placeholder="请输入角色名称" v-model="queryParams.roleName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="margin-left:30px;" size="small">查询</el-button>
          <el-button type="primary" @click="handleSynchro" size="small">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <section class="table-btn">
        <el-button v-if="checkAuthority('/ucenter/urole/add')" @click="handleAdd" type="primary" icon="plus" size="small">添加角色</el-button>
      </section>
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column prop="roleNo" label="编号"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="isEnable" label="是否启用" :formatter="IsEnableFormatter"></el-table-column>
        <el-table-column prop="rolePermissions" label="角色权限">
          <template slot-scope="scope">
            <span v-if="checkAuthority('/select')" @click="handleDetail(scope.row)" style="color:#1890ff; cursor: pointer;">查看详情</span>
          </template>
        </el-table-column>
        <el-table-column prop="createrName" label="创建人"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column> -->
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button v-if="checkAuthority('/ucenter/urole/modify')" type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleAuthority(scope.row)" v-if="checkAuthority('/ucenter/urolepermissionrel/bind')"
              >授权</el-button
            >
            <el-button v-if="checkAuthority('/ucenter/urole/delete/')" type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <role-modal ref="RoleModal" @ok="modalOk"></role-modal>
    <authority-modal ref="AuthorityModal" @ok="modalOk"></authority-modal>
  </section>
</template>

<script>
import { getRoleList, deleteRole } from './js/auth'
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import { roleIsEnable } from './js/constants'
import RoleModal from './components/RoleModal'
import AuthorityModal from './components/AuthorityModal'
import { checkAuthority } from '@/utils/btnPermission' // 页面按钮权限

export default {
  name: 'RoleList',
  data () {
    let userInfo = Vue.ls.get(USER_INFO)
    return {
      userInfo: userInfo,
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 0,
      queryParams: {
        roleName: ''
      },
      tableData: []
    }
  },
  components: { RoleModal, AuthorityModal },
  mounted () {
    this.getRoleList()
  },
  methods: {
    checkAuthority,
    IsEnableFormatter (row, column, cellValue) {
      return this.$base.constFormatter(roleIsEnable, cellValue)
    },
    onSubmit () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      const data = Object.assign({}, this.queryParams, params)
      this.getRoleList(data)
    },
    // 获取列表
    async getRoleList (data) {
      let params = {}
      if (data) {
        params = data
      } else {
        params = {
          pageNum: this.pageNum,
          pageSize: 10
        }
      }
      let res = await getRoleList(params)
      this.totalPageNum = res.data.total
      res = res.data.list
      this.tableData = res
    },
    // 刷新按钮
    handleSynchro () {
      this.$confirm('将手动同步所有角色信息，是否继续执行？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.getRoleList()
      })
    },
    // 删除按钮
    handleDelete (row) {
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.dataDelete(row)
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    },
    async dataDelete (row) {
      let params = {}
      let res = await deleteRole(row.roleNo, params)
      if (res.code === 0) {
        this.$message.success('删除成功')
      } else {
        this.$message.warning(res.msg)
      }
      this.getRoleList()
    },
    handleDetail (row) {
      this.$refs.AuthorityModal.title = '授权'
      this.$refs.AuthorityModal.isAdd = false
      this.$refs.AuthorityModal.edit(row)
    },
    // 编辑按钮
    handleEdit (row) {
      this.$refs.RoleModal.title = '角色编辑'
      this.$refs.RoleModal.isAdd = false
      this.$refs.RoleModal.edit(row)
    },
    // 授权按钮
    handleAuthority (record) {
      this.$refs.AuthorityModal.title = '授权'
      this.$refs.AuthorityModal.isAdd = true
      this.$refs.AuthorityModal.edit(record)
    },
    // 新增按钮
    handleAdd () {
      this.$refs.RoleModal.title = '角色新增'
      this.$refs.RoleModal.isAdd = true
      this.$refs.RoleModal.add()
    },
    pageHandleChange (page) {
      this.pageNum = page
      this.getRoleList()
    },
    // 业务弹框回调
    modalOk () {
      this.getRoleList()
    }
  }
}
</script>

<style lang="scss" scoped>
.table-btn {
  margin-bottom: 15px;
}
</style>
